//
//  DengLuViewController.m
//  NC-Trendy
//
//  Created by ljd on 16/2/24.
//  Copyright © 2016年 liang. All rights reserved.
//

#import "DengLuViewController.h"

#import "NCTrendyHeader.pch"

#import "UserModel.h"

#import "WoDeTableViewController.h"

#import "NSString+Password.h"

#import "ZhuCeViewController.h"

@interface DengLuViewController ()
@property (weak, nonatomic) IBOutlet UITextField *yongHuMingField;
@property (weak, nonatomic) IBOutlet UITextField *miMaField;
@property (nonatomic,strong)NSMutableArray *userMutArray;
@property(nonatomic,strong)UserModel *userModel;
@end

@implementation DengLuViewController

//MD5 方法加密
-(NSString *)jiaMi {
    return [self.miMaField.text MD5];
}

//懒加载模型数组
-(NSMutableArray *)userMutArray {
    if (!_userMutArray) {
        _userMutArray = [NSMutableArray array];
    }
    return _userMutArray;
}


//封装UIAlertView方法，直接调用就可以显示提示
-(void)xianShiTiShiFangFa:(NSString *)tiShi {
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:tiShi delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
}

#pragma mark -用户登录执行的方法
-(void)dengLuFangFa {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"正在登录";
    //如果用户名或者密码为空，提示用户输入正确的信息
    if ([self.yongHuMingField.text isEqualToString:@""]||[self.miMaField.text isEqualToString:@""]) {
        [self xianShiTiShiFangFa:@"用户名和密码为空,请输入正确的用户名和密码"];
    }
    //设置发送请求的网络路径
    NSString *wangLuoLuJing = [NSString stringWithFormat:@"http://localhost:8888/nongchao/Home/User/login"];
    //找出单例sessionManager
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    //定义一个请求头字典
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    //将用户输入的信息传入请求头键对用的值
    parameters[@"info"] = self.yongHuMingField.text;
    parameters[@"pwd"] = self.jiaMi;
    
    //sessionManager发送POST请求
    [sessionManager POST:wangLuoLuJing parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //如果成功
        NSLog(@"%@",responseObject);
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:responseObject];
        [self panDuanShiFouDengLuChengGong:dic];
        
    }//失败
                 failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            NSLog(@"有错误 %@",error);
        }
    }];
}

#pragma mark -判断是否登录成功
-(void)panDuanShiFouDengLuChengGong:(NSDictionary *)dic {

    if ([dic[@"status"] isEqualToString:@"1"]) {
        //登录成功
        //将返回的数据用模型存储并且设定为单例,用来传参
        AppDelegate *appdalegate = [AppDelegate shareAppDelegate];
        UserModel *userModel = [[UserModel alloc]initWithDic:dic[@"data"]];
        appdalegate.userModel = userModel;
        //登录成功跳转回到“我的”页面
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        //如果登录不成功，显示php返回的response的data
        [self xianShiTiShiFangFa:dic[@"data"]];
    }
    //菊花隐藏
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

//登录按键
- (IBAction)dengLuBtnClick:(UIButton *)sender {
    [self dengLuFangFa];
    
}
//跳转注册页面
- (IBAction)zhuCeBtnClick:(UIButton *)sender {
    self.yongHuMingField.text = nil;
    self.miMaField.text = nil;
    [self.navigationController pushViewController:[[ZhuCeViewController alloc]init] animated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //设置密码不是明文
    self.miMaField.secureTextEntry = YES;
    
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
